package com.haojiang.mh.remote.ui.dialog

import android.content.DialogInterface
import android.os.Bundle
import android.view.*
import android.widget.LinearLayout
import androidx.fragment.app.FragmentActivity
import com.haojiang.mh.R
import com.haojiang.mh.remote.base.BaseDialogFrag
import com.haojiang.mh.remote.utils.BleUtil.Companion.devDisconnectAll
import com.haojiang.mh.remote.utils.BleUtil.Companion.onTouchListener
import kotlinx.android.synthetic.main.frag_dialog_motor.*

class MotorTestDialogFrag : BaseDialogFrag() {

    /**
     * 伴生函数
     */
    companion object {
        //        private var GROUP_TYPE = "group_type"
        val instance by lazy {
            MotorTestDialogFrag()
        }

        fun show(fragmentActivity: FragmentActivity) {
            val fm = fragmentActivity.supportFragmentManager
            fm.executePendingTransactions()
            fm.beginTransaction().remove(instance)
            instance.show(fm, MotorTestDialogFrag::class.java.simpleName)
        }
    }


    private val onClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            if (v != null) {
                when (v.id) {
                    R.id.ivClose -> {
                        dismiss()
                        devDisconnectAll()
                    }
                }
            }
        }
    }


//    private var groupType = GROUP_TYPE_NONE


    override fun onSubCreate(savedInstanceState: Bundle?) {
        setStyle(STYLE_NO_TITLE, R.style.fragment_dialog)
//        groupType = arguments!!.getInt(GROUP_TYPE)
    }

    override fun onSubCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.frag_dialog_motor, container, false)
    }

    override fun onSubViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog!!.setCancelable(false) //是否可以点击取消
        dialog!!.setCanceledOnTouchOutside(false) //dialog外面是否可以点击

        val params = dialog!!.window!!.attributes
        val context = context
        context?.let {
            //            params.width = (Util.getScreenWidth(context) * 0.7f).toInt()
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT
            params.gravity = Gravity.CENTER
            //高度自己定义
            dialog!!.window!!.setLayout(params.width, params.height)
        }
    }


    override fun onSubActivityCreated(savedInstanceState: Bundle?) {

        ivClose.setOnClickListener(onClickListener)
        btnHeadUp.setOnTouchListener(onTouchListener)
        btnHeadDown.setOnTouchListener(onTouchListener)


        dialog!!.setOnKeyListener(object : DialogInterface.OnKeyListener {
            override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    LogUtil.e("马达测试退出")
                    devDisconnectAll()
//                    return true
                }
                return false
            }
        })

//        this.setOnKeyListener((view1, i, keyEvent) -> {
//            if (i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_UP) {
//                Navigation.findNavController(view1).navigateUp();
//                return true;
//            }
//            return false;
//        });
    }

    override fun initDestroy() {
    }


}